package com.operate.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.operate.dao.ScreenDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/4/12 21:08
 */
@RestController
public class TestController {

    @Autowired
    private ScreenDataMapper screenMapper;


    @GetMapping("/test")
    public void test() {

    }

    public static void main(String[] args) throws InterruptedException {
        String query_url = "https://open.feishu.cn/open-apis/bitable/v1/apps/U1SqbSxP5aEqHFsy5NMcur73nNd/tables/tblDGRkv53P2PwHI/records";
        String del_url = "https://open.feishu.cn/open-apis/bitable/v1/apps/U1SqbSxP5aEqHFsy5NMcur73nNd/tables/tblDGRkv53P2PwHI/records/batch_delete";
        String result = HttpUtil.createGet(query_url)
                //这个请求头.header是自己项目需要加的，可以省略
                .header("Authorization", "Bearer u-d9kKv9d_ZcQoUbD8_OXIPlkhkIa104r9h0w0gkg02H4o")
                .execute()
                //返回参数格式utf-8
                .charset("utf-8")
                .body();


        JSONObject jsonObject = JSONObject.parseObject(result);
        String code = jsonObject.getString("code");
        if ("0".equals(code)) {
            JSONObject data = jsonObject.getJSONObject("data");
            Integer total = data.getInteger("total");
            if (total > 0) {
                List<String> del = new ArrayList<>();
                JSONArray items = data.getJSONArray("items");
                for (Object item : items) {
                    JSONObject recordInfo = (JSONObject) item;
                    String record_id = recordInfo.getString("record_id");
                    del.add(record_id);
                }
                Map<String, List> delParam = new HashMap<>();
                delParam.put("records", del);

                String postResult = HttpUtil.createPost(del_url)
                        //这个请求头.header是自己项目需要加的，可以省略
                        .header("Content-Type", "application/json")
                        //这两个请求头是项目需要加的，可以省略
                        .header("Authorization", "Bearer u-d9kKv9d_ZcQoUbD8_OXIPlkhkIa104r9h0w0gkg02H4o")
                        //传输参数
                        .body(JSONObject.toJSONString(delParam))
                        .execute()
                        .body();

                System.out.println(postResult);


            }
        }
    }


}
